/*********************************************************************************
 * Software License Agreement (BSD License)
 * Copyright (c) 2012, Carlos D. Correa (info@ngraph.org)
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of netzen nor the
 *    names of its contributors may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *********************************************************************************/

#include <stdio.h>
#include "viewManager.h"
#include <QMouseEvent>
#include <QTimer>
#include <QPainter>
#include <assert.h>

#include "qNRender.h"
#include "../mouseUtils.h"

QGLWidget *widget;

QNRender::QNRender(Controller *controller, QWidget *parent):QGLWidget(parent) {
  this->controller = controller;
  assert(controller);
  setMouseTracking(true);

  controller->setRenderUI(this);
  widget = this;
  QTimer *timer = new QTimer(this);
  connect(timer, SIGNAL(timeout()), this, SLOT(update()));
  // timer->start(10);
}

QNRender::~QNRender() {
}

void QNRender::redraw() {
  repaint();
}

void QNRender::initializeGL() {
  glEnable(GL_LINE_SMOOTH);
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
}

void QNRender::update() {
  	controller->getViewManager()->tic();
	repaint();
}

bool renderingPixmap = false;
void QNRender::paintGL() {
	controller->getViewManager()->render();
}

void QNRender::resizeGL(int width, int height) {
  //printf("Resize to %d x %d\n", width, height);
	controller->getViewManager()->resize(width, height);
}

void QNRender::keyPressEvent(QKeyEvent *event) {
  //printf("Key: %d\n", event->key());
  QString s = event->text();
  if(s.length()>0) {
	  controller->getViewManager()->handleKey(s.toAscii().at(0), 0, 0);
  }
}

void QNRender::mousePressEvent(QMouseEvent *event) {
//	printf("QNRender Press\n");
  setFocus(Qt::MouseFocusReason);
  int button = 0;
  int modifiers = 0;
  int state = NZ_DOWN;
  button |= event->button()&Qt::LeftButton? NZ_LEFT_BUTTON:button;
  button |= event->button()&Qt::RightButton? NZ_RIGHT_BUTTON:button;
  button |= event->button()&Qt::MidButton? NZ_MIDDLE_BUTTON:button;

  //printf("event modifiers %d ctrl %d\n", (int) event->modifiers(), (int) Qt::ControlModifier);
  modifiers |= event->modifiers()&Qt::ControlModifier?NZ_ACTIVE_CTRL:modifiers;
  modifiers |= event->modifiers()&Qt::ShiftModifier?NZ_ACTIVE_SHIFT:modifiers;
  modifiers |= event->modifiers()&Qt::AltModifier?NZ_ACTIVE_ALT:modifiers;
  modifiers |= event->modifiers()&Qt::MetaModifier?NZ_ACTIVE_META:modifiers;

	controller->getViewManager()->mouseEvent(event->x(), event->y(), button, state, modifiers );
  repaint();
}
void QNRender::mouseDoubleClickEvent(QMouseEvent */*event*/) {
}
void QNRender::mouseReleaseEvent(QMouseEvent *event) {
//	printf("QNRender Release\n");
  int button = 0;
  int modifiers = 0;
  int state = NZ_UP;
  button |= event->button()&Qt::LeftButton? NZ_LEFT_BUTTON:button;
  button |= event->button()&Qt::RightButton? NZ_RIGHT_BUTTON:button;
  button |= event->button()&Qt::MidButton? NZ_MIDDLE_BUTTON:button;

  modifiers |= event->modifiers()&Qt::ShiftModifier?NZ_ACTIVE_SHIFT:modifiers;
  modifiers |= event->modifiers()&Qt::ControlModifier?NZ_ACTIVE_CTRL:modifiers;
  modifiers |= event->modifiers()&Qt::AltModifier?NZ_ACTIVE_ALT:modifiers;
  modifiers |= event->modifiers()&Qt::MetaModifier?NZ_ACTIVE_META:modifiers;

  	controller->getViewManager()->mouseEvent(event->x(), event->y(), button, state, modifiers);
  repaint();
}

void QNRender::mouseMoveEvent(QMouseEvent *event) {
	int button = 0;
	button |= event->buttons()&Qt::LeftButton? NZ_LEFT_BUTTON:button;
	button |= event->buttons()&Qt::RightButton? NZ_RIGHT_BUTTON:button;
	button |= event->buttons()&Qt::MidButton? NZ_MIDDLE_BUTTON:button;

	int modifiers = 0;
  modifiers |= event->modifiers()&Qt::ShiftModifier?NZ_ACTIVE_SHIFT:modifiers;
  modifiers |= event->modifiers()&Qt::ControlModifier?NZ_ACTIVE_CTRL:modifiers;
  modifiers |= event->modifiers()&Qt::AltModifier?NZ_ACTIVE_ALT:modifiers;
  modifiers |= event->modifiers()&Qt::MetaModifier?NZ_ACTIVE_META:modifiers;

//printf("QNRender MouseMove btn %d\n", button);
  	controller->getViewManager()->mouseMoveEvent(event->x(), event->y(), button, modifiers);
  repaint();
}
